﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.OleDb;

namespace EM.OA.WebCommon.Extension
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, String text, String actionName, String controllerName, Object htmlAttributes)
        {
            String input = String.Format("<input type='submit' onclick='javascript:this.form.action=\"/{0}/{1}\"' value='{2}' #htmlAttributes# />", controllerName, actionName, text);
            String value = AddHtmlAttributes(input, htmlAttributes);
            MvcHtmlString htmlString = MvcHtmlString.Create(value);
            return htmlString;
        }

        public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper, String text, String actionName, String controllerName)
        {
            return ActionButton(htmlHelper, text, actionName, controllerName, new { });
        }

        public static MvcHtmlString Button(this HtmlHelper htmlHalper, String id, String text, Object htmlAttributes)
        {
            String input = String.Format("<input type='button' id='{0}' value='{1}' #htmlAttributes# />", id, text);
            String value = AddHtmlAttributes(input, htmlAttributes);
            MvcHtmlString htmlString = MvcHtmlString.Create(value);
            return htmlString;
        }

        public static String AddHtmlAttributes(String input, Object htmlAttributes)
        {
            Type type = htmlAttributes.GetType();
            StringBuilder attributes = new StringBuilder();
            foreach (var property in type.GetProperties())
            {
                Object propertyName = property.Name;
                Object propertyValue = property.GetValue(htmlAttributes, null);
                attributes.AppendFormat(" {0}='{1}'", propertyName, propertyValue);
            }
            String value = input.Replace("#htmlAttributes#", attributes.ToString());
            return value;
        }

    }
}